<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          placeholder="请输入表描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="handleGenTable"
          v-hasPermi="['web:tool:gen:single:code']"
        >
          生成
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Upload"
          @click="openImportTable"
          v-hasPermi="['web:tool:gen:single:import']"
        >
          导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleEditTable"
          v-hasPermi="['web:tool:gen:single:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['web:tool:gen:single:remove']"
        >
          删除
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @query-table="getList" />
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" width="55" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template #default="{ $index }">
          <span>{{ formatIndex(pageParams.pageNum, pageParams.pageSize, $index) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="表名称"
        align="center"
        prop="tableName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="表描述"
        align="center"
        prop="tableComment"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="实体" align="center" prop="className" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />
      <el-table-column label="操作" align="center" width="330">
        <template #default="scope">
          <el-tooltip content="预览" placement="top">
            <el-button
              link
              type="primary"
              icon="View"
              @click="handlePreview(scope.row)"
              v-hasPermi="['web:tool:gen:single:preview']"
            />
          </el-tooltip>
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleEditTable(scope.row)"
              v-hasPermi="['web:tool:gen:single:edit']"
            />
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['web:tool:gen:single:remove']"
            />
          </el-tooltip>
          <el-tooltip content="生成代码" placement="top">
            <el-button
              link
              type="primary"
              icon="Download"
              @click="handleGenTable(scope.row)"
              v-hasPermi="['web:tool:gen:single:code']"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="pageParams.pageNum"
      v-model:limit="pageParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :title="preview.title"
      v-model="preview.open"
      width="80%"
      top="5vh"
      append-to-body
      class="scrollbar"
    >
      <el-tabs v-model="preview.activeName">
        <el-tab-pane
          v-for="(value, key) in preview.data"
          :label="key.substring(key.lastIndexOf('/') + 1, key.indexOf('.vm'))"
          :name="key.substring(key.lastIndexOf('/') + 1, key.indexOf('.vm'))"
          :key="value"
        >
          <el-link
            :underline="false"
            icon="DocumentCopy"
            v-copyText="value"
            v-copyText:callback="copyTextSuccess"
            style="float: right"
            >&nbsp;复制
          </el-link>
          <pre>{{ value }}</pre>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <import-table ref="importRef" @ok="handleQuery" />
  </div>
</template>

<script setup name="Gen">
  import { ref, onActivated, getCurrentInstance, toRefs, reactive } from 'vue';
  import { useRoute } from 'vue-router';
  import { listTable, previewTable, delTable, genCode } from '@/api/tool/gen';
  import router from '@/router';
  import importTable from './importTable';
  import download from '@/plugins/download';

  const route = useRoute();
  const { proxy } = getCurrentInstance();

  const tableList = ref([]);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const tableNames = ref([]);
  const dateRange = ref([]);
  const uniqueId = ref('');

  const data = reactive({
    queryParams: {
      tableName: undefined,
      tableComment: undefined,
    },
    pageParams: {
      pageNum: 1,
      pageSize: 10,
    },
    preview: {
      open: false,
      title: '代码预览',
      data: {},
      activeName: 'controller',
    },
  });

  const { queryParams, pageParams, preview } = toRefs(data);

  onActivated(() => {
    const time = route.query.t;
    if (time != null && time != uniqueId.value) {
      uniqueId.value = time;
      queryParams.value.pageNum = Number(route.query.pageNum);
      dateRange.value = [];
      proxy.resetForm('queryForm');
      getList();
    }
  });

  /** 查询表集合 */
  function getList() {
    loading.value = true;
    listTable(
      pageParams.value,
      proxy.addFieldTimeRange(queryParams.value, dateRange.value, 'createTime'),
    ).then((response) => {
      if (response.data) {
        tableList.value = response.data.records ? response.data.records : [];
        total.value = response.data.total ? response.data.total : 0;
      } else {
        tableList.value = [];
        total.value = 0;
      }
      loading.value = false;
    });
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    pageParams.value.pageNum = 1;
    getList();
  }

  /** 生成代码操作 */
  function handleGenTable(row) {
    const selectIds = row.tableId ? [row.tableId] : ids.value;
    if (selectIds.length === 0) {
      proxy.$modal.msgError('请选择要生成的数据');
      return;
    }
    if (row.genType === '1') {
      genCode(row.tableName).then(() => {
        proxy.$modal.msgSuccess('成功生成到自定义路径：' + row.genPath);
      });
    } else {
      download.zip('/web/tool/gen/single/batchDownloadCode', 'base.zip', selectIds, {});
    }
  }

  /** 打开导入表弹窗 */
  function openImportTable() {
    proxy.$refs['importRef'].show();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    dateRange.value = [];
    proxy.resetForm('queryRef');
    handleQuery();
  }

  /** 预览按钮 */
  function handlePreview(row) {
    previewTable(row.tableId).then((response) => {
      preview.value.data = response.data;
      preview.value.open = true;
      preview.value.activeName = 'controller';
    });
  }

  /** 复制代码成功 */
  function copyTextSuccess() {
    proxy.$modal.msgSuccess('复制成功');
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.tableId);
    tableNames.value = selection.map((item) => item.tableName);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 修改按钮操作 */
  function handleEditTable(row) {
    const tableId = row.tableId || ids.value[0];
    router.push({
      path: '/tool/gen-edit/index/' + tableId,
      query: { pageNum: pageParams.value.pageNum },
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const tableIds = row.tableId ? [row.tableId] : ids.value;
    proxy.$modal
      .confirm('是否确认删除表编号为"' + tableIds + '"的数据项？')
      .then(function () {
        return delTable(tableIds);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess('删除成功');
      })
      .catch(() => {});
  }

  getList();
</script>
